<?php
/**
 * Created by PhpStorm.
 * User: songques
 * Date: 2018/4/11
 * Time: 11:57
 */

namespace veda\util;

use think\Session;

class Tools
{
    //时间格式化输出
    public static $TIMEFORMAT='Y-m-d H:i:s';

    //分页 单面数
    public static $PAGE_NUMBER=2;

    //生成uuid
    public static function uuid() {
        if (function_exists ( 'com_create_guid' )) {
            return com_create_guid ();
        } else {
            mt_srand ( ( double ) microtime () * 10000 ); //optional for php 4.2.0 and up.随便数播种，4.2.0以后不需要了。
            $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间（微秒计）生成唯一id.
            $hyphen = chr ( 45 ); // "-"
            $uuid = '' . //chr(123)// "{"
                substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );
            //.chr(125);// "}"
            $uuid=strtolower($uuid);
            return $uuid;
        }
    }

    //登入验证
    public static function checkLogin($validate_session=null,$level=''){

        $global_url='http://'.$_SERVER['HTTP_HOST'].'/admin/login';
        if($validate_session==null){
            header('Location: '.$global_url);
            exit();
        }

    }

    //生成标号
    public static function buildNumber($numberValue=''){

        if(strlen($numberValue)==3){

            $top = substr($numberValue,0,1);
            $body=substr($numberValue,1,2);
            if($top==''||$body==''){
                $numberValue='A01';
                return $numberValue;
            }


            $body=(int)$body;
            $body=$body+1;

            if ($body<=99){
                if($body<10){
                    $body='0'.$body;
                }
                else
                    $body=''.$body;

            }else{

                $body='01';
                $top=ord($top);
                if($top<90){
                    $top=$top+1;
                }
                else{
                    $top=65;
                }
                $top=chr($top);
            }

            $numberValue=$top.''.''.$body;
            return $numberValue;
        }else{
            return 'A01';
        }


    }

    public static function printTicket($name='',$sex='',$age='',$part='',$quick='',$id='',$wait='',$p_number='',$time='',$file_name=''){
        $path_Template=ROOT_PATH.'public/ticket/Template';
        $path_png=$path_Template.'/ticket_basic.png';
        $path_font='font/微软雅黑.ttf';

        $path_dic=ROOT_PATH.'public/ticket/';

        $image = \think\Image::open($path_png);

        $image->text($name,
            $path_font,
            7,
            '#000000',
            array(41,27));

        $image->text($sex,
            $path_font,
            7,
            '#000000',
            array(110,27));

        $image->text($age,
            $path_font,
            7,
            '#000000',
            array(41,48));

        $image->text($part,
            $path_font,
            7,
            '#000000',
            array(110,48));

        $image->text($quick,
            $path_font,
            7,
            '#000000',
            array(41,66));

        $image->text($id,
            $path_font,
            7,
            '#000000',
            array(110,68));

        $image->text($wait,
            $path_font,
            15,
            '#000000',
            array(120,86));

        $image->text($p_number,
            $path_font,
            20,
            '#000000',
            array(50,138));

        $image->text($time,
            $path_font,
            7,
            '#000000',
            array(57,189));

        $image->save($path_dic.$file_name.'.png');

    }


}